package youcaihuaoa.weetop.com.vm.apply

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.OtaServiceFactory
import api.UserServiceFactory
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.*
import com.robot.base.util.LiveDataBus
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import youcaihuaoa.weetop.com.R
import java.io.File
import java.net.URLEncoder

class MyApplyProfileViewModel(application: Application) : GLBaseViewModel(application) {


    val userName = ObservableField("")
    val departmentName = ObservableField("")
    val jobName = ObservableField("")
    val companyName = ObservableField("")
    val complete = ObservableField(false)
    val uiEvent = UIEvent()
    var id = ""

    //0 新申请 1 申请详情 2审批详情
    val status = ObservableField(0)

    val submitField = ObservableField("提交")
    val cancelField = ObservableField("取消")


    val bkType = arrayListOf("全天", "上班卡", "下班卡")
    var type = ""
    val examine_remark = ObservableField("")
    val qxVisibility = ObservableField(false)
    var qx_menu_data = ""

    class UIEvent {
        val menuDataLiveEvent = SingleLiveEvent<CustomProfileBean>()
        val skipFileEvent = SingleLiveEvent<Void>()
        val confirmEvent = SingleLiveEvent<Void>()
        val updateFileEvent = SingleLiveEvent<List<FileBean>>()
        val infoEvent = SingleLiveEvent<List<MyApplyInfoBean>>()
        val qxDataEvent = SingleLiveEvent<List<QxsxDataBean>>()
    }

    init {
        userName.set(PrefsManager.getUserInfo().user_name)
        companyName.set(PrefsManager.getUserInfo().company_name)
        departmentName.set(PrefsManager.getUserInfo().department_name)
        jobName.set(PrefsManager.getUserInfo().job_name)
    }

    val toSkipFileCommand = BindingCommand<Any>(BindingAction {
        uiEvent.skipFileEvent.call()
    })

    val confirmCommand = BindingCommand<Any>(BindingAction {
        when (status.get()) {
            0, 1 -> {
                uiEvent.confirmEvent.call()
            }
            2 -> {
                examineUserVerify(1)
            }
        }

    })

    val cancelCommand = BindingCommand<Any>(BindingAction {
        when (status.get()) {
            0 -> {
                finish()
            }
            1 -> {
                examineUserRevoke()
            }
            2 -> {
                examineUserVerify(-1)
            }
        }
    })

    fun initData(status: Int, id: String, type: String) {
        this.status.set(status)
        this.id = id
        this.type = type
        initData(type, status != 2)
        when (status) {
            0 -> {
                submitField.set("提交")
                cancelField.set("返回")
                if (type == "7") {
                    qxVisibility.set(true)
                    templateQxsxMenuData()
                }
                templateTrendsGetData(type)
            }
            1 -> {
                submitField.set("重新发起")
                cancelField.set("撤销")
                applyUserInfo()
            }
            2 -> {
                submitField.set("同意")
                cancelField.set("拒绝")
                examineUserInfo()
            }
        }
    }

    fun initData(type: String, enable: Boolean, customProfileBean: CustomProfileBean? = null) {
        val infoBeanList = mutableListOf<MyApplyInfoBean>()
        when (type) {
            "1" -> {
                //请假申请
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "请假类型:",
                        key = "qjlx_data",
                        type = "0",
                        text = customProfileBean?.qjlx_data ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开始时间:",
                        key = "start_time_dg",
                        type = "1_time",
                        text = customProfileBean?.start_time_dg ?: "",
                        enable = enable

                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "结束时间:",
                        key = "end_time_dg",
                        type = "1_time",
                        text = customProfileBean?.end_time_dg ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "请假时长(小时):",
                        key = "hour_num_dg",
                        type = "0",
                        text = customProfileBean?.hour_num_dg ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "请假事由:",
                        key = "reason_dg",
                        type = "0",
                        text = customProfileBean?.reason_dg ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "2" -> {
                //加班申请
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开始时间:",
                        key = "start_time_dg",
                        type = "1_time",
                        text = customProfileBean?.start_time_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "结束时间:",
                        key = "end_time_dg",
                        type = "1_time",
                        text = customProfileBean?.end_time_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "加班时长(小时):",
                        key = "hour_num_dg",
                        type = "0",
                        text = customProfileBean?.hour_num_dg ?: "",
                        enable = enable,
                        inputType = 1

                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "加班原因:",
                        key = "reason_dg",
                        type = "0",
                        text = customProfileBean?.reason_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "3" -> {
                //外勤申请
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开始时间:",
                        key = "start_time_dg",
                        type = "1_time",
                        text = customProfileBean?.start_time_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "结束时间:",
                        key = "end_time_dg",
                        type = "1_time",
                        text = customProfileBean?.end_time_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "外勤时长(小时):",
                        key = "hour_num_dg",
                        type = "0",
                        text = customProfileBean?.hour_num_dg ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "外勤事由:",
                        key = "reason_dg",
                        type = "0",
                        text = customProfileBean?.reason_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "4" -> {
                //补卡申请
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "补卡类型:",
                        key = "bk_type",
                        type = "bk_type",
                        text = if (TextUtils.isEmpty(customProfileBean?.bk_type) || customProfileBean?.bk_type == "0") {
                            ""
                        } else {
                            bkType[(customProfileBean?.bk_type ?: "1").toInt() - 1]
                        },
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "补卡日期:",
                        key = "bk_day",
                        type = "1",
                        text = customProfileBean?.bk_day ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "补卡时间:",
                        key = "bk_time",
                        type = "1_time",
                        text = customProfileBean?.bk_time ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "补卡事由:",
                        key = "reason_dg",
                        type = "0",
                        text = customProfileBean?.reason_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "5" -> {
                //转正申请
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "转正日期:",
                        key = "zz_time",
                        type = "1",
                        text = customProfileBean?.zz_time ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "6" -> {
                //调薪
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "当前薪资:",
                        key = "tx_dq_price",
                        type = "0",
                        text = customProfileBean?.tx_dq_price ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "调动薪资:",
                        key = "tx_td_price",
                        type = "0",
                        text = customProfileBean?.tx_td_price ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "调薪日期:",
                        key = "tx_start_time",
                        type = "1",
                        text = customProfileBean?.tx_start_time ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "7" -> {
                //7权限申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "8" -> {
                //8离职申请】
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "离职日期:",
                        key = "lz_day",
                        type = "1",
                        text = customProfileBean?.lz_day ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "9" -> {
                //，行政【9资料申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "申请时间:",
                        key = "zl_start_time",
                        type = "1_time",
                        text = customProfileBean?.zl_start_time ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "申请备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "10" -> {
                //10用印申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "用印时间:",
                        key = "yy_start_time",
                        type = "1_time",
                        text = customProfileBean?.yy_start_time ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "用印备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "11" -> {
                //11工序审批申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "申请内容:",
                        key = "gx_content",
                        type = "0",
                        text = customProfileBean?.gx_content ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "12" -> {
                //12合同审批申请】，
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "申请内容:",
                        key = "ht_content",
                        type = "0",
                        text = customProfileBean?.ht_content ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "13" -> {
                //财务【13报销申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "报销金额:",
                        key = "bx_price",
                        type = "0",
                        text = customProfileBean?.bx_price ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "费用发生日期:",
                        key = "bx_fyrq_day",
                        type = "1",
                        text = customProfileBean?.bx_fyrq_day ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "费用类型:",
                        key = "bx_fylx_title",
                        type = "0",
                        text = customProfileBean?.bx_fylx_title ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "费用说明:",
                        key = "bx_content",
                        type = "0",
                        text = customProfileBean?.bx_content ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "14" -> {
                //14开票申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开票单位名称:",
                        key = "kp_title",
                        type = "0",
                        text = customProfileBean?.kp_title ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开票单位税号:",
                        key = "kp_sh",
                        type = "0",
                        text = customProfileBean?.kp_sh ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开票邮箱:",
                        key = "kp_email",
                        type = "0",
                        text = customProfileBean?.kp_email ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开票金额:",
                        key = "kp_price",
                        type = "0",
                        text = customProfileBean?.kp_price ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开票备注:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "15" -> {
                //15财务审核申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "款项用途:",
                        key = "cw_content_dg",
                        type = "0",
                        text = customProfileBean?.cw_content_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "用款部门:",
                        key = "cw_ykbm_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_ykbm_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "借款人/经办人:",
                        key = "cw_jkr_title",
                        type = "0",
                        text = customProfileBean?.cw_jkr_title ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "付款金额:",
                        key = "cw_fk_price_dg",
                        type = "0",
                        text = customProfileBean?.cw_fk_price_dg ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "款项详细说明:",
                        key = "cw_info_data_dg",
                        type = "0",
                        text = customProfileBean?.cw_info_data_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "收款单位:",
                        key = "cw_shdw_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_shdw_title_dg ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "银行账号:",
                        key = "cw_yhkh_number_dg",
                        type = "0",
                        text = customProfileBean?.cw_yhkh_number_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开户行:",
                        key = "cw_khh_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_khh_title_dg ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "支付方式:",
                        key = "cw_zffs_pay_dg",
                        type = "0",
                        text = customProfileBean?.cw_zffs_pay_dg ?: "",
                        enable = enable
                    )
                )

                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "16" -> {
                //16劳务结算申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "用款部门:",
                        key = "cw_ykbm_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_ykbm_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "付款金额:",
                        key = "cw_fk_price_dg",
                        type = "0",
                        text = customProfileBean?.cw_fk_price_dg ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "收款单位:",
                        key = "cw_shdw_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_shdw_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "银行账号:",
                        key = "cw_yhkh_number_dg",
                        type = "0",
                        text = customProfileBean?.cw_yhkh_number_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开户行:",
                        key = "cw_khh_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_khh_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "支付方式:",
                        key = "cw_zffs_pay_dg",
                        type = "0",
                        text = customProfileBean?.cw_zffs_pay_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "履约说明:",
                        key = "cw_info_data_dg",
                        type = "0",
                        text = customProfileBean?.cw_info_data_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "17" -> {
                //17工会经费申请；
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "款项用途:",
                        key = "cw_content_dg",
                        type = "0",
                        text = customProfileBean?.cw_content_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "用款部门:",
                        key = "cw_ykbm_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_ykbm_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "付款金额:",
                        key = "cw_fk_price_dg",
                        type = "0",
                        text = customProfileBean?.cw_fk_price_dg ?: "",
                        enable = enable,
                        inputType = 1
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "收款单位:",
                        key = "cw_shdw_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_shdw_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "银行账号:",
                        key = "cw_yhkh_number_dg",
                        type = "0",
                        text = customProfileBean?.cw_yhkh_number_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "开户行:",
                        key = "cw_khh_title_dg",
                        type = "0",
                        text = customProfileBean?.cw_khh_title_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "支付方式:",
                        key = "cw_zffs_pay_dg",
                        type = "0",
                        text = customProfileBean?.cw_zffs_pay_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
            "18" -> {
                //18薪酬绩效申请】
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "事项说明:",
                        key = "cw_info_data_dg",
                        type = "0",
                        text = customProfileBean?.cw_info_data_dg ?: "",
                        enable = enable
                    )
                )
                infoBeanList.add(
                    MyApplyInfoBean(
                        title = "备注信息:",
                        key = "public_remark",
                        type = "0",
                        text = customProfileBean?.public_remark ?: "",
                        enable = enable
                    )
                )
            }
        }
        uiEvent.infoEvent.postValue(infoBeanList)
    }

    fun templateQxsxMenuData() {
        UserServiceFactory.templateQxsxMenuData()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<QxsxDataBean>>>(this) {
                override fun onNext(t: BaseResponse<List<QxsxDataBean>>) {
                    uiEvent.qxDataEvent.postValue(t.data)
                }
            })
    }

    fun examineUserInfo() {
        UserServiceFactory.examineUserInfo(
            mapOf(
                "template_type" to type,
                "model_type" to 1,
                "id" to id
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<CustomProfileBean>>(this) {
            override fun onNext(t: BaseResponse<CustomProfileBean>) {
                t.data ?: return
                userName.set(t.data.user_name)
                companyName.set(t.data.company_name)
                departmentName.set(t.data.department_name)
                jobName.set(t.data.job_name)
                complete.set(t.data.public_status == "1")
                t.data.str_data.forEach {
                    it.viewEnable = false
                }
                uiEvent.menuDataLiveEvent.postValue(t.data)
                if (type == "7") {
                    qx_menu_data = t.data.qx_menu_data ?: ""
                    qxVisibility.set(true)
                    templateQxsxMenuData()
                }
                initData(type, false, t.data)
                val list = t.data.public_annex_path?.split(";")
                val fileBeanList = mutableListOf<FileBean>()
                list?.forEach {
                    val fileBean = FileBean()
                    fileBean.file_path = it
                    fileBean.file_type = it.substring(it.indexOf(".") + 1)
                    fileBeanList.add(fileBean)
                }
                uiEvent.updateFileEvent.postValue(fileBeanList)
            }
        })
    }

    fun applyUserInfo() {
        UserServiceFactory.applyUserInfo(
            mapOf(
                "template_type" to type,
                "model_type" to 1,
                "id" to id
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<CustomProfileBean>>(this) {
            override fun onNext(t: BaseResponse<CustomProfileBean>) {
                t.data ?: return
                userName.set(t.data.user_name)
                companyName.set(t.data.company_name)
                departmentName.set(t.data.department_name)
                jobName.set(t.data.job_name)

                complete.set(t.data.public_status == "1")
                t.data.str_data.forEach {
                    it.viewEnable = t.data.public_status != "1"
                }

                uiEvent.menuDataLiveEvent.postValue(t.data)
                if (type == "7") {
                    qx_menu_data = t.data.qx_menu_data ?: ""
                    qxVisibility.set(true)
                    templateQxsxMenuData()
                }
                initData(type, t.data.public_status != "1", t.data)
                val list = t.data.public_annex_path?.split(";")
                val fileBeanList = mutableListOf<FileBean>()
                list?.forEach {
                    val fileBean = FileBean()
                    fileBean.file_path = it
                    fileBean.file_type = it.substring(it.indexOf(".") + 1)
                    fileBeanList.add(fileBean)
                }
                uiEvent.updateFileEvent.postValue(fileBeanList)
            }
        })
    }

    fun examineUserVerify(examine_status: Int) {
        val map = hashMapOf<String, Any>(
            "model_type" to 1,
            "template_type" to type,
            "examine_status" to examine_status
        )

        if (!TextUtils.isEmpty(id)) {
            map["id"] = id
        }
        if (!TextUtils.isEmpty(examine_remark.get())) {
            map["examine_remark"] = examine_remark.get() ?: ""
        }
        UserServiceFactory.examineUserVerify(
            map
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_APPLY).postValue("1")
                finish()
            }
        })
    }

    fun examineUserRevoke() {
        val map = hashMapOf<String, Any>(
            "model_type" to 1,
            "template_type" to type
        )
        if (!TextUtils.isEmpty(examine_remark.get())) {
            map["revoke_remark"] = examine_remark.get() ?: ""
        }

        if (!TextUtils.isEmpty(id)) {
            map["id"] to id
        }
        UserServiceFactory.examineUserRevoke(
            map
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_APPLY).postValue("1")
                finish()
            }
        })
    }

    fun templateTrendsGetData(template_type: String) {
        val params = mapOf("template_type" to template_type)
        UserServiceFactory.templateTrendsGetData(params)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<CustomProfileBean>>(this) {
                override fun onNext(t: BaseResponse<CustomProfileBean>) {
                    uiEvent.menuDataLiveEvent.postValue(t.data)
                }
            })
    }

    fun uploadFile(file: File) {
        val requestFile =
            file.asRequestBody("application/octet-stream".toMediaTypeOrNull())
        val part = MultipartBody.Part.createFormData(
            "file",
            URLEncoder.encode(file.name, "UTF-8"),
            requestFile
        )
        OtaServiceFactory.uploadWhole(part)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<FileBean>>(this) {
                override fun onNext(t: BaseResponse<FileBean>) {
                    uiEvent.updateFileEvent.postValue(arrayListOf(t.data))
                }
            })
    }

    fun confirm(map: Map<String, Any>) {
        when (type) {
            "1" -> {
                //请假申请
                UserServiceFactory.templateAskUserAdd(map)
            }
            "2" -> {
                //加班申请
                UserServiceFactory.templateShiftUserAdd(map)
            }
            "3" -> {
                //外勤申请
                UserServiceFactory.templateFieldUserAdd(map)
            }
            "4" -> {
                //补卡申请
                UserServiceFactory.templateCardUserAdd(map)
            }
            "5" -> {
                //转正申请
                UserServiceFactory.templateBecomeUserAdd(map)
            }
            "6" -> {
                //调薪
                UserServiceFactory.templateRaiseUserAdd(map)
            }
            "7" -> {
                //7权限申请；
                UserServiceFactory.templateAuthorityUserAdd(map)
            }
            "8" -> {
                //8离职申请】
                UserServiceFactory.templateDepartUserAdd(map)
            }
            "9" -> {
                //，行政【9资料申请；
                UserServiceFactory.templateInformationUserAdd(map)
            }
            "10" -> {
                //10用印申请；
                UserServiceFactory.templateSealUserAdd(map)
            }
            "11" -> {
                //11工序审批申请；
                UserServiceFactory.templateProcessUserAdd(map)
            }
            "12" -> {
                //12合同审批申请】，
                UserServiceFactory.templateCovenantUserAdd(map)
            }
            "13" -> {
                //财务【13报销申请；
                UserServiceFactory.templateExpenseUserAdd(map)

            }
            "14" -> {
                //14开票申请；
                UserServiceFactory.templateInvoicingUserAdd(map)
            }
            "15" -> {
                //15财务审核申请；
                UserServiceFactory.templateTreasurerUserAdd(map)
            }
            "16" -> {
                //16劳务结算申请；
                UserServiceFactory.templateLaborUserAdd(map)
            }
            "17" -> {
                //17工会经费申请；
                UserServiceFactory.templateTradeUserAdd(map)
            }
            "18" -> {
                //18薪酬绩效申请】
                UserServiceFactory.templateSalaryUserAdd(map)
            }
            else -> {
                UserServiceFactory.templateAskUserAdd(map)
            }
        }.subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                ToastUtils.showShort("申请成功")
                finish()
            }
        })
    }

}